﻿package 
{
	import flash.display.MovieClip;
	import flash.events.*;
	import fl.controls.TextInput;

	public class Settings_mc extends MovieClip
	{
		private static var user:TextInput;
		private static var api:TextInput;

		private var first_run:Boolean;


		public function Settings_mc(firstRun=false)
		{
			first_run = firstRun;

			this.dragBar.addEventListener(MouseEvent.MOUSE_DOWN, drag_me, false, 0, true);

			user = user_id;
			api = api_key;

			var settings = new Settings();

			this.SaveSettings.addEventListener(MouseEvent.CLICK, saveSettings, false, 0, true);
			this.SaveSettings.buttonMode = true;

			this.Cancel.addEventListener(MouseEvent.CLICK, hideSettings, false, 0, true);
			this.Cancel.buttonMode = true;
			
			if(settings.get_setting("user_id"))
			{
				user.text = settings.get_setting("user_id");
			}
			if(settings.get_setting('api_key'))
			{
				api.text = settings.get_setting('api_key');
			}
		}

		function hideSettings(e)
		{
			this.parent.removeChild(e.target.parent);
		}

		function drag_me(e:MouseEvent)
		{
			stage.nativeWindow.startMove();
		}


/* Save the settings, it the checkbox is selected then lets save it */
		private function saveSettings(e:MouseEvent):void
		{
			var settings = new Settings();
			settings.insert( "user_id", user.text );
			settings.insert( "api_key", api.text );
			//Saved or Edited preferences
			if(first_run)
			{
				Intervals.get_projects();
			}
			this.parent.removeChild(e.target.parent);
		}


//		When the application loads, check the settings to see if anything has changed, if it has, set the value
//		of the new values to the values of the inputs 
//		
		public static function checkSettings(filename)
		{
			var settings = new Settings();
			try
			{
				Main.set_preference('user_id', settings.get_setting("user_id"));
				Main.set_preference('api_key', settings.get_setting("api_key"));
			}
			catch(e)
			{
				// no settings yet
			}
		}


		public static function get_settings(setting:String)
		{
			return prefs.getValue( setting );
		}
	}
}